package com.aforce.managers;

import java.util.ArrayList;
import java.util.List;

import javax.media.j3d.BranchGroup;

import com.aforce.entities.AbstractEntity;
import com.aforce.entities.Ship;

/** EntityManager is responsible for keeping track of all the entities on the map **/
public class EntityManager
{
	private static boolean initialized = false;

	Ship userShip = null;
	final List<AbstractEntity> entities = new ArrayList<AbstractEntity>();
	final BranchGroup root;
	
	public EntityManager(BranchGroup root)
	{
		if(initialized) throw new RuntimeException("You may not initialize multiple instances of EntityManager");
		initialized = true;
		
		this.root = root;
		new Clicker(entities).start();
	}
	
	public void addEntity(AbstractEntity entity)
	{
		BranchGroup bg = new BranchGroup();
		bg.addChild(entity.getTransformGroup());
		root.addChild(bg);
		entities.add(entity);
	}
	
	public void removeEntity(AbstractEntity entity)
	{
		entities.remove(entity);
	}
	
	public void setUserShip(Ship ship)
	{
		userShip = ship;
	}
	
	public Ship getUserShip()
	{
		return userShip;
	}

}

class Clicker extends Thread
{
	public List<AbstractEntity> entities;
	
	public Clicker(List<AbstractEntity> entities)
	{
		this.entities = entities;
	}
	
	public void run()
	{
		while(true)
		{
			if(!TimeManager.paused)
				for(AbstractEntity entity : entities)
				{
					entity.update();
				}
	
			try{Thread.sleep(100);}catch(Exception e){}
		}
	}
}

